package com.meitianhui.convenitenbuy.module.store.presenter;

import com.google.gson.Gson;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.base.BaseResponse;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.http.ResponseObserver;
import com.meitianhui.convenitenbuy.module.order.model.bean.EmptyInfo;
import com.meitianhui.convenitenbuy.module.store.contract.CouponPayContract;
import com.meitianhui.convenitenbuy.module.store.http.StoreApiDataSource;
import com.meitianhui.convenitenbuy.module.store.model.CouponCardInfo;
import com.meitianhui.convenitenbuy.module.store.ui.activity.CouponPayActivity;
import com.meitianhui.convenitenbuy.util.DateUtil;
import com.meitianhui.convenitenbuy.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

public class CouponPayPresenter implements CouponPayContract.Presenter {

    private CouponPayActivity mContext;
    private CouponPayContract.View mView;

    public CouponPayPresenter(CouponPayContract.View view, CouponPayActivity context) {
        this.mView = view;
        this.mContext = context;
    }

    @Override
    public void couponExchange(String num, CouponCardInfo.Coupon coupon) {
        Map<String, String> map = new HashMap<>();
        Map<String, String> params = new HashMap<>();
        params.put("data_source", Constants.CUSTOM_APP);
        params.put("payment_way_key", Constants.PAYMENT_WAY_06);
        params.put("detail", "兑换商品");//优惠券标题
        params.put("amount", StringUtils.moneyMul(num, coupon.getMarket_price_voucher()));
        params.put("currency_code", "CNY");
        params.put("buyer_id", InfoProvider.getUserToken(App.getInstance()).getMember_id());
        params.put("seller_id", Constants.SELLER_ID);
        params.put("order_type_key", "DDLX_02");
        params.put("transaction_date", DateUtil.getyyyyMMddHHmmss());
        Map<String, String> params2 = new HashMap<>();
        params2.put("item_id", coupon.getItem_id());
        params2.put("member_id", InfoProvider.getUserToken(App.getInstance()).getMember_id());
        params2.put("member_type_key", "consumer");
        params2.put("num", num);
        params.put("out_trade_body", new Gson().toJson(params2));
        String paramJson = new Gson().toJson(params);

        StoreApiDataSource.getInstance()
                .couponExchange(InfoProvider.getUserToken(App.getInstance()).getUser_token(),
                        "finance.orderPay", paramJson)
                .subscribe(new ResponseObserver<BaseResponse<EmptyInfo>>(mContext, true) {
                    @Override
                    public void onSuccess(BaseResponse<EmptyInfo> response) {
                        mView.couponExchangeSuccess();
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        mView.onFail(code, msg);
                    }
                });
    }

    @Override
    public void attachView(Object view) {
    }

    @Override
    public void detachView() {
    }

}
